<template>
  <div>
    <BasicTable @register="tableRegister" @row-click="rowClick">
      <template #toolbar>
        <Tooltip>
          <template #title>
            <span>{{ ADD }}</span>
          </template>
          <SvgIcon style="cursor: pointer" name="plus" @click="openAddModal" />
        </Tooltip>
      </template>
      <template #bodyCell="{ column, record }">
        <template v-if="column.key === 'action'">
          <TableAction
            :actions="[
              {
                tooltip: EDIT,
                icon: 'edit|svg',
                onClick: openUpdateModal.bind(null, record),
                auth: 'system-role-update',
              },
              {
                tooltip: DELETE,
                icon: 'delete|svg',
                popConfirm: {
                  title: `确定要删除：${record.name}吗？`,
                  okText: SURE,
                  cancelText: CANCEL,
                  confirm: handleDel.bind(null, record),
                },
                auth: 'system-role-delete',
              },
            ]"
          />
        </template>
      </template>
    </BasicTable>
    <SaveModal @register="saveDictionaryModalRegister" @on-ok="onOk" />
  </div>
</template>

<script lang="ts" setup>
  import { unref } from 'vue';
  import { Tooltip } from 'ant-design-vue';
  import { BasicTable, useTable, TableAction } from '/@/components/Table';
  import { SvgIcon } from '/@/components/Icon';
  import SaveModal from './save-dictionary-modal.vue';
  import {
    dictionaryColumns as columns,
    getDictionaryFormConfig as getFormConfig,
  } from '../schema';
  import { getList, del } from '../api';
  import { ACTION, ADD, EDIT, DELETE, SURE, CANCEL } from '/@/constant';
  import { useModal } from '/@/components/Modal';
  import { FormData } from '/@/model/FormData';
  import { FormTypeEnum } from '/@/enums/formTypeEnum';
  import { DictionaryListModel, DictionarySaveModel } from '/@/views/system/dictionary/model';

  const emit = defineEmits(['select-change']);

  const [tableRegister, { reload }] = useTable({
    title: '字典列表',
    columns,
    api: getList,
    rowKey: 'id',
    showTableSetting: true,
    useSearchForm: true,
    formConfig: getFormConfig(),
    handleSearchInfoFn: (searchInfo) => {
      return searchInfo;
    },
    actionColumn: {
      width: 160,
      title: ACTION,
      dataIndex: 'action',
    },
    rowSelection: {
      type: 'radio',
    },
  });

  const [saveDictionaryModalRegister, { openModal: openSaveDictionaryModal }] = useModal();
  const openAddModal = () => {
    const data: FormData<DictionarySaveModel> = {
      type: FormTypeEnum.ADD,
    };
    openSaveDictionaryModal(true, data);
  };
  const openUpdateModal = (obj: DictionaryListModel) => {
    const data: FormData<DictionaryListModel> = {
      type: FormTypeEnum.EDIT,
      obj,
    };
    openSaveDictionaryModal(true, data);
  };
  const handleDel = async (record: Recordable) => {
    const { id } = unref(record);
    await del({ id });
    await reload();
  };
  const onOk = () => {
    reload();
  };

  const rowClick = (record) => {
    emit('select-change', record);
  };
</script>

<style scoped></style>
